var express = require("express");
var router = express.Router();
/* GET home page. */
const path = require("path");
const fs = require("fs");
const util = require("util");
const unlink = util.promisify(fs.unlink);

const { uploadFile } = require("../util/upload");

router.get("/", function (req, res, next) {
  res.send({});
});

router.delete("/delImg", function (req, res, next) {
  const { url } = req.body;
  const imgPath = path.join(path.resolve(__dirname, "../public"), url);
  unlink(imgPath);
  res.send({});
});

router.post("/img", async function (req, res, next) {
  const { data } = await uploadFile(req);
  res.send(data);
});

router.post("/updateImg", async function (req, res, next) {
  const { data } = await uploadFile(req);
  res.send(data);
});
router.post("/updateimages", async function (req, res, next) {
  const { data } = await uploadFile(req);
  res.send(data);
});

router.post("/images", async function (req, res, next) {
  const { data } = await uploadFile(req);
  res.send(data);
});

module.exports = router;
